package com.zcp.swpierefreshlayout;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/3/4.
 */

public class RecycleAdapter extends RecyclerView.Adapter<RecycleAdapter.MyViewHolder> {

    ArrayList<String> arr;
    LayoutInflater inflater;

    public RecycleAdapter(Context context, ArrayList<String> arr) {
        this.inflater = LayoutInflater.from(context);
        this.arr = arr;

    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.recycle_item, parent, false);
        MyViewHolder mv = new MyViewHolder(view);
        return mv;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.tv.setText(arr.get(position));

    }

    @Override
    public int getItemCount() {
        return arr.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        private final TextView tv;

        public MyViewHolder(View itemView) {
            super(itemView);
            tv = (TextView) itemView.findViewById(R.id.tv);
        }
    }

    public void addItem(ArrayList<String> list) {
        list.addAll(arr);
        arr.removeAll(arr);
        arr.addAll(list);
        notifyDataSetChanged();
    }

    public void addMoreItem(ArrayList<String> titles) {
        arr.addAll(titles);
        notifyDataSetChanged();
    }
}
